<?php
/**
 * Created by PhpStorm.
 * User: bevan
 * Date: 2018/1/6
 * Time: 下午2:59
 */

namespace app\common\model;

use think\Model;

class MenuModel extends Model
{
    protected $readonly = ['create_time'];

    /**
     *
     * @method  获取子菜单
     * @version
     * @User: bevan
     * @param $id
     * @return
     */
    public function getChildList($id, $rule_arr)
    {
        $menus = $this->where('parent_id', $id)
            ->alias('m')
            ->field('*,m.name as name,m.name as label')
            ->select()
            ->toArray();
        foreach ($menus as $key => $val) {
            $tmp = $this->where('parent_id', $val['id'])
                ->alias('m')
                ->field('*,m.name as name,m.name as label')
                ->select()
                ->toArray(); //临时存放
            $menus = array_merge($menus, $tmp);
        }
        $menus = $this->treeMenu($menus, $id, 0, $rule_arr);
        foreach ($menus as $key => &$val) {
            if (!$val['children'] && $val['type'] != 3) {
                unset($menus[$key]);
            }
        }
        return $menus;
    }

    /**
     *
     * @method  根据role_id获取菜单详细
     * @version
     * @User: thans
     * @param $id
     * @return
     */
    public function getByRoleId($id, $field = '*')
    {
        return $this->where('rule_id', $id)->field($field)->find();
    }

    /**
     *
     * @method  根据role数组获取菜单详细
     * @version
     * @User: thans
     * @param $id
     * @return
     */
    public function getByRoleIds($ids, $field = '*')
    {
        return $this->where('rule_id', 'in', $ids)->field($field)->select();
    }

    /**
     *
     * @method 通过菜单主键找到对应规格的id
     * @version
     * @User: bevan
     * @param $id
     * @return rule_id
     */
    public function getRuleIdByMenuId($id)
    {
        return $this->where('id', $id)->value('rule_id');
    }

    /**
     *
     * @method 获取所有的菜单 树状结构
     * @version
     * @User: bevan
     * @param array $rule_arr
     * @return
     */
    public function getTreeList($rule_arr = [])
    {
        $menus = $this
            ->alias('m')
            ->field('*,m.name as name,m.name as label')
            ->select();
        return $this->treeMenu($menus, 0, 0, $rule_arr);
    }

    /**
     *
     * @method  增加额外的参数
     * @version
     * @User: beva
     * @param $data  二维数组 在每个一维数组下增加额外的参数
     * @param $key_name
     * @param $type
     * @return
     */
    public function addExtraParam($data, $key_name, $type)
    {
        foreach ($data as &$item) {
            switch ($type) {
                case 'array':
                    $item[$key_name] = [];
                    break;
            }
        }
        return $data;
    }

    /**
     *
     * @method  树状排序
     * @version
     * @User: bevan
     * @param array $menus
     * @param int $pId
     * @param int $deep
     * @param array $rule_arr
     * @return
     */
    public function treeMenu($menus = [], $pId = 0, $deep = 0, $rule_arr = [], $is_tree = true)
    {
        $treeMenu = [];
        foreach ($menus as $k => $menu) {
            if ($menu['parent_id'] == $pId) {
                if ($rule_arr) {
                    if (in_array($menu['rule_id'], $rule_arr)) {
                        $menu['checked'] = 1;
                    } else {
                        $menu['checked'] = 0;
                    }
                } else {
                    $menu['checked'] = 1;
                }
                $menu['deep']    = $deep;
                $menu['label']   = str_repeat('└―', $deep) . $menu['name'];
                $menu['path']    = $menu['app'] . '/' . $menu['controller'] . '/' . $menu['action'];
                $url             = url($menu['app'] . '/' . $menu['controller'] . '/' . $menu['action']);
                $menu['endpath'] = $menu['param'] ? $url . '?' . $menu['param'] : $url;
                if ($is_tree) {
                    unset($menus[$k]);
                    $key_name        = 'children';
                    $menu[$key_name] = $this->treeMenu($menus, $menu['id'], $deep + 1, $rule_arr, $is_tree);
                    $treeMenu[]      = $menu;
                } else {
                    unset($menus[$k]);
                    $treeMenu[] = $menu;
                    $m          = $this->treeMenu($menus, $menu['id'], $deep + 1, $rule_arr, false);
                    if ($m) {
                        foreach ($m as $v) {
                            $treeMenu[] = $v;
                        }
                    }
                }
            }
        }
        return $treeMenu;
    }

    /**
     *
     * @method 所有的菜单排序
     * @version
     * @User: bevan
     * @return
     */
    public function getSortList($data)
    {
        if ($data['keyword']) {
            $menus = $this->alias('m')
                ->field('*,m.name as name,m.name as label')
                ->where('name', 'like', '%' . $data['keyword'] . '%')
                ->select();
        } else {
            $menus = $this->alias('m')
                ->field('*,m.name as name,m.name as label')
                ->select();
        }

        $treemenu = $this->treeMenu($menus, 0, 0, [], false);

        return $treemenu;
    }
    public function toggle($id)
    {
        $res = $this->where('id', $id)
            ->find();
        if ($res['status'] == 1) {
            $res = $this->where('id', $id)->setField('status', 0);
        } else {
            $res = $this->where('id', $id)->setField('status', 1);
        }
        return $res;
    }
}
